package com.cjt.chat.service.impl;

import com.cjt.chat.mapper.GroupMapper;
import com.cjt.chat.mapper.UserMapper;
import com.cjt.chat.pojo.Group;
import com.cjt.chat.pojo.GroupMessage;
import com.cjt.chat.pojo.User;
import com.cjt.chat.service.GroupService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author cjt
 * @since 2024/3/29
 */
@Service
public class GroupServiceImpl implements GroupService {
    @Resource
    private GroupMapper groupMapper;
    @Resource
    private UserMapper userMapper;

    @Override
    public List<Group> getGroupList(Integer userId) {
        List<Integer> groupIdList = groupMapper.getGroupIdList(userId);
        if (groupIdList != null && !groupIdList.isEmpty()) {
            return groupMapper.getGroupList(groupIdList);
        }
        return null;
    }

    @Override
    public List<User> getGroupMemberList(Integer groupId) {
        List<Integer> groupMemberIdList = groupMapper.getGroupMemberIdList(groupId);
        return userMapper.queryUsersByUseridList(groupMemberIdList);
    }

    @Override
    public List<GroupMessage> getGroupMessage(Integer groupId, Integer page) {
        return groupMapper.getGroupMessage(groupId, page * 30);
    }

    @Override
    public void addGroupMember(Integer groupId, Integer userId) {
        groupMapper.addGroupMember(groupId, userId, 2);
    }

    @Override
    public boolean removeGroupMember(Integer groupId, Integer userId, Integer memberId/*要移除的用户id*/) {
        Integer role = groupMapper.getGroupMemberRole(groupId, userId);
        if (role == 0) {
            groupMapper.removeGroupMember(groupId, memberId);
            return true;
        }
        return false;
    }

    @Override
    public boolean setGroupNotice(Integer groupId, String notice, Integer userId) {
        Integer role = groupMapper.getGroupMemberRole(groupId, userId);
        if (role == 0) {
            groupMapper.setGroupNotice(groupId, notice);
            return true;
        }
        return false;
    }

    @Override
    public void saveGroupMessage(GroupMessage groupMessage) {
        groupMapper.saveGroupMessage(groupMessage);
    }

    @Override
    public Group searchGroupInfo(Integer userId, Integer groupId) {
        Group group = groupMapper.queryGroupByGroupId(groupId);
        if (group != null) {
            Integer number = groupMapper.queryGroupNumber(groupId);
            Boolean isIn = groupMapper.queryUserInGroup(userId, groupId) != null;
            group.setGroupNumber(number);
            group.setInGroup(isIn);
            return group;
        }
        return null;
    }

    @Override
    public Integer getMemberRole(Integer memberId, Integer groupId) {
        return groupMapper.getGroupMemberRole(groupId, memberId);
    }

    @Override
    public void quitGroup(Integer userId, Integer groupId) {
        groupMapper.removeGroupMember(groupId, userId);
    }

    @Override
    public void deleteGroup(Integer groupId, Integer userId) {
        Integer role = groupMapper.getGroupMemberRole(groupId, userId);
        if (role == 0) {
            groupMapper.deleteGroupMessageById(groupId);
            groupMapper.deleteGroupMemberById(groupId);
            groupMapper.deleteGroupById(groupId);
        }
    }

    @Override
    public void updateGroupName(Integer userId, Integer groupId, String groupName) {
        Integer role = groupMapper.getGroupMemberRole(groupId, userId);
        if (role == 0) {
            groupMapper.updateGroupName(groupId, groupName);
        }
    }

    @Override
    public void createGroup(Integer userId, String groupName) {
        long time = System.currentTimeMillis();
        Group group = new Group();
        group.setCreatorId(userId);
        group.setGroupName(groupName);
        group.setCreateTime(time);
        groupMapper.createGroup(group);
        groupMapper.addGroupMember(group.getGroupId(), userId, 0);
    }
}
